package 一六;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by lenovo on 2017/3/14.
 */
public class q6 {

    private static List<String> lists = new ArrayList<>();

    /**
     * @param array 待测试数组
     * @param x     待测位置横坐标
     * @param y     待测位置纵坐标
     * @return 这个位置是否成功
     */
    private static Boolean checkSingle(Integer[][] array, int x, int y) {

        if (x - 1 >= 0 && Math.abs(array[x - 1][y] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (x + 1 < 3 && Math.abs(array[x + 1][y] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (y - 1 >= 0 && Math.abs(array[x][y - 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (y + 1 < 4 && Math.abs(array[x][y + 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (x - 1 >= 0 && y - 1 >= 0 && Math.abs(array[x - 1][y - 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (x + 1 < 3 && y + 1 < 4 && Math.abs(array[x + 1][y + 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (x - 1 >= 0 && y + 1 < 4 && Math.abs(array[x - 1][y + 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        if (x + 1 < 3 && y - 1 >= 0 && Math.abs(array[x + 1][y - 1] - array[x][y]) == 1) {
            return Boolean.FALSE;
        }

        return Boolean.TRUE;
    }

    /**
     * 字符串数组转换成目标矩阵
     *
     * @param origin
     * @return
     */
    private static Integer[][] chage(String[] origin) {

        Integer[][] result = new Integer[3][4];
        result[0][0] = -1;
        result[2][3] = 10;

        result[0][1] = Integer.parseInt(origin[0].substring(0, 1));
        result[0][2] = Integer.parseInt(origin[0].substring(1, 2));
        result[0][3] = Integer.parseInt(origin[0].substring(2, 3));

        result[1][0] = Integer.parseInt(origin[1].substring(0, 1));
        result[1][1] = Integer.parseInt(origin[1].substring(1, 2));
        result[1][2] = Integer.parseInt(origin[1].substring(2, 3));
        result[1][3] = Integer.parseInt(origin[1].substring(3, 4));

        result[2][0] = Integer.parseInt(origin[2].substring(0, 1));
        result[2][1] = Integer.parseInt(origin[2].substring(1, 2));
        result[2][2] = Integer.parseInt(origin[2].substring(2, 3));

        return result;
    }

    /**
     * 递归方式组合字符串
     *
     * @param source
     * @param result
     * @param lists
     */
    private static void fullPermution(Vector<Character> source, Vector<Character> result, List<String> lists) {

        if (source.size() == 0) {
            String str = "";
            for (int i = 0; i < result.size(); i++) {
                str += result.elementAt(i);
            }
            lists.add(str);
        }

        for (int i = 0; i < source.size(); i++) {
            Vector<Character> tsource = new Vector<>(source);
            Vector<Character> tresult = new Vector<>(result);
            tresult.add(source.elementAt(i));
            tsource.remove(i);
            q6.fullPermution(tsource, tresult, lists);
        }
    }

    public static void main(String[] args) {

        int count = 0;
        Vector<Character> sourse = new Vector<>();
        Vector<Character> result = new Vector<>();

        for (int i = 0; i < 10; i++) {
            sourse.add((char) ('0' + i));
        }

        q6.fullPermution(sourse, result, lists);

        String[] str = new String[3];
        for (String list : lists) {
            str[0] = list.substring(0, 3);
            str[1] = list.substring(3, 7);
            str[2] = list.substring(7, 10);

            Integer[][] rs =q6.chage(str);

            boolean flag = false;
            for(int i=0;i<3;i++){
                for(int j=0;j<4;j++){
                    if(!q6.checkSingle(rs,i,j)){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    break;
                }
            }
            if(!flag){
                count++;
            }

        }

        System.out.println(count);
    }
}
